﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using CookComputing.XmlRpc;
using Curchy.MetablogCMS.Models;
using Curchy.MetablogCMS.Persistance;

namespace Curchy.MetablogCMS.Handlers
{
    public class MetaWeblog : XmlRpcService, IMetaWeblog
    {
        #region IMetaWeblog Members

        public string AddPost(string blogid, string username, string password, Post post, bool publish)
        {
            if (ValidateUser(username, password))
            {
                return MetablogPersistance.AddPost(blogid, username, post);
            }
            throw new XmlRpcFaultException(0, "User is not valid!");
        }

        public bool UpdatePost(string postid, string username, string password, Post post, bool publish)
        {
            if (ValidateUser(username, password))
            {
                if (string.IsNullOrEmpty(postid))
                    throw new XmlRpcFaultException(0, "This post is not yet created, please create a new one!");

                MetablogPersistance.UpdatePost(postid, username, post);
                return true;
            }
            throw new XmlRpcFaultException(0, "User is not valid!");
        }

        public Post GetPost(string postid, string username, string password)
        {
            if (ValidateUser(username, password))
            {
                return MetablogPersistance.GetPost(new Guid(postid));
            }
            throw new XmlRpcFaultException(0, "User is not valid!");
        }

        public CategoryInfo[] GetCategories(string blogid, string username, string password)
        {
            if (ValidateUser(username, password))
            {
                List<CategoryInfo> categoryInfos = new List<CategoryInfo>();

                return categoryInfos.ToArray();
            }
            throw new XmlRpcFaultException(0, "User is not valid!");
        }

        public Post[] GetRecentPosts(string blogid, string username, string password, int numberOfPosts)
        {
            if (ValidateUser(username, password))
            {
                var posts = MetablogPersistance.GetPosts(blogid, numberOfPosts).ToArray();
                return posts;
            }
            throw new XmlRpcFaultException(0, "User is not valid!");
        }

        public MediaObjectInfo NewMediaObject(string blogid, string username, string password,
            MediaObject mediaObject)
        {
            if (ValidateUser(username, password))
            {
                Guid g = Guid.NewGuid();
                string url = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath + "metablog/media/" + g.ToString();;
                MetablogPersistance.AddMedia(blogid, mediaObject, g, url);

                MediaObjectInfo objectInfo = new MediaObjectInfo();
                objectInfo.url = url;
                return objectInfo;
            }
            throw new XmlRpcFaultException(0, "User is not valid!");
        }

        public bool DeletePost(string key, string postid, string username, string password, bool publish)
        {
            if (ValidateUser(username, password))
            {
                MetablogPersistance.RemovePost(new Guid(postid));
                return true;
            }
            throw new XmlRpcFaultException(0, "User is not valid!");
        }

        public BlogInfo[] GetUsersBlogs(string key, string username, string password)
        {
            if (ValidateUser(username, password))
            {
                List<BlogInfo> infoList = new List<BlogInfo>();

                BlogInfo bi = new BlogInfo();
                bi.blogid = ConfigurationManager.AppSettings["BlogId"];
                bi.blogName = ConfigurationManager.AppSettings["BlogName"];
                bi.url = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath;
                infoList.Add(bi);

                return infoList.ToArray();
            }
            throw new XmlRpcFaultException(0, "User is not valid!");
        }

        public UserInfo GetUserInfo(string key, string username, string password)
        {
            if (ValidateUser(username, password))
            {
                UserInfo info = new UserInfo();
                
                // Fill Information

                return info;
            }
            throw new XmlRpcFaultException(0, "User is not valid!");
        }

        #endregion

        #region Private Methods

        private bool ValidateUser(string username, string password)
        {
            return ConfigurationManager.AppSettings["username"] == username && ConfigurationManager.AppSettings["password"] == password;
        }

        #endregion
        
    }
}
